--
-- Author: gnar
-- Date: 2015-12-31 18:43:15
--
local ManagerCookie = {}


enumlCookieFlag = {
    publicBox = 1001,
    characterDataForChoose = 1002,
    characterDataForMap = 1003,
    characterDataForFight = 1004,
    bagInfo = 1005,
    heroBaseInfos = 1006,
    heroDetails = 1007,
    taskInfo = 1008,

    shortLivedData = 10001, -- {lastChooseCharacterId,lastLoginId}
}

function ManagerCookie.isKeyExist(flag) 

    local isKeyExist = false
    for _, v in pairs(enumlCookieFlag) do
        if v == flag then
            isKeyExist = true
            break
        end
    end

    return isKeyExist

end

function ManagerCookie.getValue(flag, prefixName) 
    
    if ManagerCookie.isKeyExist(flag) == false then
        print("ManagerCookie.getValue unExistKey",flag)
        return  
    end
    local prefixName = prefixName or ManagerUserData.userData.userBaseInfo.userId
    local path = device.writablePath .. prefixName .. "_" .. flag .. ".lua"
    print("ManagerCookie.getValue",path,flag)
	if not io.exists(path) then
        print("ManagerCookie.getValue unExistPath",flag)
		return 
	end 

    local result = dofile(path)
    --local result = loadstring("return "..str)()  

    print("ManagerCookie.getValue ",result)
	return result

end


function ManagerCookie.setValue(flag, value, prefixName)

    if ManagerCookie.isKeyExist(flag) == false then
        print("ManagerCookie.setValue unExistKey",flag,value)
        return  
    end
    local prefixName = prefixName or ManagerUserData.userData.userBaseInfo.userId
	local path = device.writablePath .. prefixName .. "_" .. flag .. ".lua"

    print("ManagerCookie.setValue",path,flag)
    local file = io.open(path, "w+")
    if file then
        if  file:write("Cookie=" .. serialize(value) .. "\nreturn Cookie") == nil then 
            print("ManagerCookie.setValue writeFail",flag,value)
            return 
        end
        file:flush()
        file:close()
        return true
    else
        print("ManagerCookie.setValue unExitFile",flag,value)
        return 
    end
end
return ManagerCookie
